<?php
class Icd10DataSource {
	protected 	$_db = '';
	protected 	$_table = '';
	
	protected   $_field = '';
	function Icd10DataSource() {
		$this->_db 		= 	$GLOBALS['frame']['adodb']['db'];
		$this->_table 	= 	array('icd10_group', 'icd10');
		
		$locale       =   L10nHelper::getLocale();
        $this->_field =   L10nHelper::changeField($locale, 'name');
	}
	
	public function getAllGroups() {
				
		
		$sql = "SELECT icd10_group_id id, code, $this->_field AS name
				FROM {$this->_table['0']}";
		return $result = $this->_db->GetAll($sql);
	}
	
    public function getGroupsByChapterId($chapterId) {        
        $sql = "SELECT t.icd10_group_id id, t.code, t.$this->_field AS name
                FROM {$this->_table['0']} t 
                    INNER JOIN icd10_chapter r ON (t.icd10_chapter_id = r.icd10_chapter_id)                    
                WHERE t.icd10_chapter_id='$chapterId'";        
        $result = $this->_db->GetAll($sql);              
        return $result;
    }
    
	public function getBlocks($group_id = 0) {		
		$sql = "SELECT icd10_id id, code, t.$this->_field name
				FROM {$this->_table['1']} t 				    
				WHERE LENGTH(code) = 3";
		if ($group_id > 0) {
			$sql .= " AND icd10_group_id = $group_id";
		}
		
		return $result = $this->_db->GetAll($sql);
	}
	
	public function getItems($code = '') {				
		$sql = "SELECT icd10_id id, code, t.$this->_field name
				FROM {$this->_table['1']} t"; 				    
		if ($code != '') {
			$sql .= " WHERE code LIKE '$code%'";
		}
		return $result = $this->_db->GetAll($sql);	
	}
	
}
?>